package com.atguigu.acl.controller;


import com.atguigu.acl.entity.ResourceCategory;
import com.atguigu.acl.service.ResourceCategoryService;
import com.atguigu.common.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  资源分类控制
 * </p>
 *
 * @author smday
 * @since 2020-08-10
 */
@RestController
@RequestMapping("/resourceCategory")
public class ResourceCategoryController {

    @Autowired
    private ResourceCategoryService resourceCategoryService;

    @ApiOperation("查询所有后台资源分类")
    @GetMapping("/listAll")
    public Result listAll() {
        List<ResourceCategory> resourceList = resourceCategoryService.listAll();
        return Result.ok().data("resourceList",resourceList);
    }

    @ApiOperation("添加后台资源分类")
    @PostMapping("/create")
    public Result create(@RequestBody ResourceCategory resourceCategory) {
        int count = resourceCategoryService.create(resourceCategory);
        if (count > 0) {
            return Result.ok().data("count",count);
        } else {
            return Result.error();
        }
    }

    @ApiOperation("修改后台资源分类")
    @PostMapping("/update/{id}")
    public Result update(@PathVariable Long id,
                               @RequestBody ResourceCategory resourceCategory) {
        int count = resourceCategoryService.update(id, resourceCategory);
        if (count > 0) {
            return Result.ok().data("count",count);
        } else {
            return Result.error();
        }
    }

    @ApiOperation("根据ID删除后台资源")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        int count = resourceCategoryService.delete(id);
        if (count > 0) {
            return Result.ok().data("count",count);
        } else {
            return Result.error();
        }
    }

}

